package cn.iocoder.yudao.module.biobee.controller.admin.tasklist;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biobee.controller.admin.tasklist.vo.*;
import cn.iocoder.yudao.module.biobee.dal.dataobject.tasklist.TaskListDO;
import cn.iocoder.yudao.module.biobee.service.tasklist.TaskListService;

@Tag(name = "管理后台 - 任务列")
@RestController
@RequestMapping("/biobee/task-list")
@Validated
public class TaskListController {

    @Resource
    private TaskListService taskListService;

    @PostMapping("/create")
    @Operation(summary = "创建任务列")
    @PreAuthorize("@ss.hasPermission('biobee:task-list:create')")
    public CommonResult<Long> createTaskList(@Valid @RequestBody TaskListSaveReqVO createReqVO) {
        return success(taskListService.createTaskList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务列")
    @PreAuthorize("@ss.hasPermission('biobee:task-list:update')")
    public CommonResult<Boolean> updateTaskList(@Valid @RequestBody TaskListSaveReqVO updateReqVO) {
        taskListService.updateTaskList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除任务列")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biobee:task-list:delete')")
    public CommonResult<Boolean> deleteTaskList(@RequestParam("id") Long id) {
        taskListService.deleteTaskList(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除任务列")
                @PreAuthorize("@ss.hasPermission('biobee:task-list:delete')")
    public CommonResult<Boolean> deleteTaskListList(@RequestParam("ids") List<Long> ids) {
        taskListService.deleteTaskListListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务列")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biobee:task-list:query')")
    public CommonResult<TaskListRespVO> getTaskList(@RequestParam("id") Long id) {
        TaskListDO taskList = taskListService.getTaskList(id);
        return success(BeanUtils.toBean(taskList, TaskListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得任务列分页")
    @PreAuthorize("@ss.hasPermission('biobee:task-list:query')")
    public CommonResult<PageResult<TaskListRespVO>> getTaskListPage(@Valid TaskListPageReqVO pageReqVO) {
        PageResult<TaskListDO> pageResult = taskListService.getTaskListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出任务列 Excel")
    @PreAuthorize("@ss.hasPermission('biobee:task-list:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskListExcel(@Valid TaskListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskListDO> list = taskListService.getTaskListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "任务列.xls", "数据", TaskListRespVO.class,
                        BeanUtils.toBean(list, TaskListRespVO.class));
    }

}